package com.example.demo.Controller;

import com.example.demo.Mapper.AssetMapper;
import com.example.demo.Mapper.UserInfoMapper;
import com.example.demo.entity.ClientAsset;
import com.example.demo.entity.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class Client {
    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    AssetMapper assetMapper;

    @RequestMapping("/client_info")
    public String clientInfo(Model model)
    {
        List<UserInfo> results=userInfoMapper.getUsersInfo();
        model.addAttribute("results",results);
        return "/manager/ClientViewer.html";
    }

    @RequestMapping("/addClient")
    public String clientAdd(Model model,@RequestParam(value = "client_id")int client_id,
                            @RequestParam(value = "client_name")String client_name,@RequestParam(value = "tel")String tel,
                            @RequestParam(value = "age")int age,@RequestParam(value = "email")String email,
                            @RequestParam(value = "client_password")String client_password)
    {
        int flag=userInfoMapper.addUserInfo(client_id,client_name,tel,age,email,client_password);
        if(flag == 1)
        {
            List<UserInfo> results=userInfoMapper.getUsersInfo();
            model.addAttribute("results",results);
            return "/manager/ClientViewer.html";
        }
        else {
            return "/error";
        }

    }

    @RequestMapping("/updateClient")
    public String clientUpdate(Model model,
                                @RequestParam(value = "client_id")int client_id,
                                @RequestParam(value = "client_name")String client_name,
                                @RequestParam(value = "tel")String tel,
                                @RequestParam(value = "age")int age,
                                @RequestParam(value = "email")String email,
                                @RequestParam(value = "client_password")String client_password)
    {
        int flag=userInfoMapper.updateUserInfo(client_id,client_name,tel,age,email,client_password);
        if(flag == 1)
        {
            List<UserInfo> results=userInfoMapper.getUsersInfo();
            model.addAttribute("results",results);
            return "/manager/ClientViewer.html";
        }
        else {
            return "/error";
        }

    }

    @RequestMapping("/asset")
    public String clientAsset(Model model)
    {
        List<ClientAsset> results=assetMapper.getAsset();
        model.addAttribute("results",results);
        return "/manager/ClientAssetViewer.html";
    }


    @RequestMapping("/delClient")
    public String clientDel(Model model,@RequestParam(value = "client_id")int client_id)
    {
        int flag = userInfoMapper.delUserInfo(client_id);
        if(flag == 1)
        {
            List<UserInfo> results=userInfoMapper.getUsersInfo();
            model.addAttribute("results",results);
            return "/manager/ClientViewer.html";
        }
        else {
            return "/error";
        }


    }





}
